/**
 * 
 */

function createXMLHttpRequest() {
  var request = false;
  if (window.XMLHttpRequest) {
    request = new XMLHttpRequest();    
  } else if (window.ActiveXObject) {
    var versions = ['Microsoft.XMLHTTP', 'MSXML.XMLHTTP', 'Microsoft.XMLHTTP',
                    'Msxml2.XMLHTTP.7.0', 'Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.5.0',
                    'Msxml2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP'];
    for (var i = 0; i < versions.length; i++) {
      try {
        request = new ActiveXObject(versions[i]);
        if (request) {
          return request;
        }
      } catch (e) {}
    }
  }
  return request;
}

function codebeauty(lang, code) {
	var url='/blog/cb?lang='+lang;
	var param="code="+encodeURIComponent(code);
	var ajax = new createXMLHttpRequest();
	ajax.open("POST",url,false);
	ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    ajax.send(param);
    return ajax.responseText;
}

function prepost(form) {
	title=trim(form.title.value);
	if(title=="") {
		alert("Title cannot be empty");
		return false;
	} else {
		return true;
	}
}

function trim(str) {
	return str.replace(/^\s*(.*?)[\s\n]*$/g, '$1');
}

function checkEbookForm(form) {
	if(form["type"][0].checked) {
		title=trim(form.title.value);
		if(title=="") {
			alert("Title cannot be empty");
			return false;
		}
		startpage=trim(form.startpage.value);
		if(startpage=="") {
			alert("Start Page cannot be empty");
			return false;
		}
	}
	else {
		bookid=form.bookid.value;
		var regu = /^[0-9]+$/;
		if(!regu.test(bookid)) {
			alert("bookid must be an integer");
			return false;
		}
	}
	return true;
}